package disjunction.examples.states
{
	import disjunction.Controller;
	import disjunction.GameState;
	import disjunction.state.StateChangeEvent;
	import disjunction.View;
	
	import flash.events.EventDispatcher;
	
	/**
	 * States Example main controller update strategy. Outlines the game loop
	 * essentials. Some functions modify the model directly, while others
	 * output an intermediate data structure for immediate consumption. The
	 * class, as Strategy DP, should not hold state (no members of any kind).
	 *
	 * @author Nick Wiggill
	 */
	
	public class PlayControlStrategy extends AbstractControllerStrategy
	{
		public override function execute(timeDeltaMs:uint, model:Object, view:View, controller:Controller):void
		{
			processEntities(model.entities);
			
			//collisionDetector.checkCollisions(model.entities);
			//collisionResolver.resolveCollisions(model.entities);
			
			resolveCollisions(getCollisions(model.entities));

			updateLevelTime(model as PlayModel, timeDeltaMs);
			checkGameOver(model.player.life, model.timeInLevelSec);
		}
		
		private function updateLevelTime(model:PlayModel, timeDeltaMs:uint):void
		{
			model.timeInLevelSec += (timeDeltaMs * 1000);
		}
		
		private function processEntities(entities:Vector.<MyEntity>):void
		{
			for each (var entity:MyEntity in entities)
			{
				produceCommands(entity); //ais to produce commands as long as no commands remain or command buffers are not full
				actOnCommands(entity); //both player and ais
			}
		}
		
		private function produceCommands(entity:MyEntity):void
		{
			
		}
		
		private function actOnCommands(entity:MyEntity):void
		{
			
		}
		
		private function getCollisions(entities:Vector.<MyEntity>):Vector.<MyEntity>
		{
			return null;//DEV
		}
		
		private function resolveCollisions(entities:Vector.<MyEntity>):void
		{
			
		}
		
		private function checkGameOver(life:Number, timeInLevelSec:Number):void
		{
			if (life <= 0)
				dispatchEvent(new StateChangeEvent(StateChangeEvent.CHANGE, "Scoreboard", null, timeInLevelSec));
		}
	}
}